package main

import (
	"fmt"
	"math/rand"
	"time"
)

func main() {
	startGame()
}

func createNum(p *int) {
	rand.Seed(time.Now().UnixNano())

	for {
		num := rand.Intn(10000)
		if num > 1000 {
			*p = num
			return
		}
	}
}
func getNum(num int, slice []int) {
	slice[0] = num / 1000
	slice[1] = num % 1000 / 100
	slice[2] = num % 100 / 10
	slice[3] = num % 10
}

func startGame() {
	var operator int
	for {
		fmt.Println("请选择：1.开始/继续 2.结束游戏")
		fmt.Scan(&operator)

		if operator == 1 {
			var num int
			createNum(&num)

			slice := make([]int, 4)
			getNum(num, slice)

			onGame(slice)
		} else {
			return
		}

	}
}

func onGame(slice []int) {
	var num int

	for {
		for {
			fmt.Println("请输入一个4位数: ")
			fmt.Scan(&num)
			if 999 < num && num < 10000 {
				break
			}
			fmt.Println("您输入的数字不符合要求")
		}

		s := make([]int, 4)
		getNum(num, s)

		n := 0
		for index, data := range slice {
			if s[index] > data {
				fmt.Printf("第%d位大了点\n", index+1)
			} else if s[index] < data {
				fmt.Printf("第%d位小了点\n", index+1)
			} else {
				fmt.Printf("第%d位猜对了\n", index+1)
				n++
			}
		}

		if n == 4 {
			fmt.Println("全部猜对了！！！")
			break
		}
	}

}
